package com.tanhua.dubbo.api;

import com.tanhua.mondb.RecommendUser;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

/*
    * 查询今日佳人
    * */

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //创建Criteria对象封装条件,按照toUserId域进行查询，在按照score域进行降序排序，取第一条
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //使用mongoTemplate进行查询
        return mongoTemplate.findOne(query, RecommendUser.class);
    }
    /*
    * 推荐列表
    * */
    @Override
    public PageResult queryRecommendationUserList(Integer page, Integer pagesize, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId));
        long count = mongoTemplate.count(query, RecommendUser.class);
        query.with(Sort.by(Sort.Order.desc("score"))).limit((page-1)*pagesize).skip(pagesize);
        List<RecommendUser> list = mongoTemplate.find(query,RecommendUser.class);
        return new PageResult(page,pagesize,count,list);
    }
}

